package com.example.myprojectoffeixue;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * Created by ASUS on 2016/11/10.
 */
public class GetPressionAdapter extends BaseAdapter {
    public TextView delete;
    public ArrayList<GateWay> user;
    public Context context;
    public PermissionsActivity pa;
    private LayoutInflater layoutInflater;
    private MyApplication m;

    public GetPressionAdapter(ArrayList<GateWay> user, Context context) {
        super();
        this.user = user;
        this.context = context;
        pa = (PermissionsActivity) context;
        m = (MyApplication) pa.getApplication();
        this.layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return user.size();
    }

    @Override
    public Object getItem(int position) {
        return user.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder h = null;
        if (convertView == null) {
            h = new ViewHolder();
            View view1 = layoutInflater.inflate(R.layout.item2_premission, null);
            View view2 = layoutInflater.inflate(R.layout.item_delete, null);
            convertView = new SwipeItemLayout(view1, view2, null, null);
            h.text_delete = (TextView) view2.findViewById(R.id.delete);
            h.mac_tv = (TextView) convertView.findViewById(R.id.Mac_text);
            h.text_get = (TextView) convertView.findViewById(R.id.delete);
            h.mobile_tv = (TextView) convertView.findViewById(R.id.mobile);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        h.text_get.setVisibility(View.VISIBLE);
        h.text_get.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                GateWay Gw = new GateWay();
                if (user.size() != 0) {
                    Gw.id = user.get(position).id;
                }
                String j = JSON.toJSONString(Gw);
                j = AESUtil.encrypt(j);
                HttpUtils httpUtils = new HttpUtils();
                RequestParams paraMyappyapps = new RequestParams();
                paraMyappyapps.setHeader("Content-Type", "application/json");
                try {
                    paraMyappyapps.setBodyEntity(new StringEntity(j, "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.confirmAuthMac, paraMyappyapps,
                        new RequestCallBack<String>() {

                            @Override
                            public void onFailure(HttpException arg0, String arg1) {
                                HttpConnection.toastLog("网络异常", context);
                            }
                            @Override
                            public void onSuccess(ResponseInfo<String> arg0) {
                                if (arg0.result != null && !("".equals(arg0.result))) {
                                    arg0.result = AESUtil.decrypt(arg0.result);
                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    Integer it = j.getInteger("status");
                                    if (0 == it) {
                                        user.get(position).status = 1;
                                        GetPressionAdapter.this.notifyDataSetChanged();
                                        m.GetDeviceList();
                                    }else{
                                        HttpConnection.toastLog("服务器错误", context);
                                    }
                                } else {
                                    HttpConnection.toastLog("服务器错误", context);
                                }
                            }
                        });
            }
        });
        h.text_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                GateWay Gw = new GateWay();
                if (user.size() != 0) {
                    Gw.id = user.get(position).id;
                }
                String j = JSON.toJSONString(Gw);
                Log.d("j", j);
                j = AESUtil.encrypt(j);
                Log.d("url", HttpConnection.deleteAuthMac);
                HttpUtils httpUtils = new HttpUtils();
                RequestParams paraMyappyapps = new RequestParams();

                paraMyappyapps.setHeader("Content-Type", "application/json");
                try {
                    paraMyappyapps.setBodyEntity(new StringEntity(j, "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.deleteAuthMac, paraMyappyapps,
                        new RequestCallBack<String>() {
                            @Override
                            public void onFailure(HttpException arg0, String arg1) {
                                HttpConnection.toastLog("网络异常", context);
                            }

                            @Override
                            public void onSuccess(ResponseInfo<String> arg0) {

                                if (arg0.result != null && !("".equals(arg0.result))) {
                                    arg0.result = AESUtil.decrypt(arg0.result);
                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                    Integer it = j.getInteger("status");
                                    if (0 == it) {
                                        user.remove(position);
                                       GetPressionAdapter.this.notifyDataSetChanged();
                                    }
                                } else {

                                    HttpConnection.toastLog("服务器错误", context);

                                }
                            }
                        });

            }
        });

        if (user.get(position).status == 0) {
            h.text_get.setText("接受");
            h.text_get.setBackgroundResource(R.drawable.bg_edittext_color);
        } else {
            h.text_get.setText("已接受");
            h.text_get.setBackgroundResource(R.drawable.stop_gate);
        }

        Log.d("fromUserId",user.get(position).fromUserId+"");
        h.mobile_tv.setText("来自：" + user.get(position).fromUserId + "的分享");
        if(user.get(position).mac!=null){
        h.mac_tv.setText(user.get(position).mac);}else{
            h.mac_tv.setText("设备集合");
        }
        return convertView;
    }
    class ViewHolder {
        LinearLayout all_item;
        TextView mac_tv;
        TextView mobile_tv;
        TextView text_delete;
        TextView text_get;
    }


}
